<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
          v-hasPerm="['factory:factory-member:add']"
          type="success"
          @click="handleOpenDialog()"
        >
          <template #icon><Plus /></template>
          新增
        </el-button>
        <el-button
          v-hasPerm="['factory:factory-member:delete']"
          type="danger"
          :disabled="removeIds.length === 0"
          @click="handleDelete()"
        >
          <template #icon><Delete /></template>
          删除
        </el-button>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column key="id" label="员工ID（主键）" prop="id" min-width="150" align="center" />
        <el-table-column
          key="employeeId"
          label="员工编号/工号"
          prop="employeeId"
          min-width="150"
          align="center"
        />
        <el-table-column key="name" label="姓名" prop="name" min-width="150" align="center" />
        <el-table-column key="gender" label="性别" prop="gender" min-width="150" align="center" />
        <el-table-column
          key="idCard"
          label="身份证号"
          prop="idCard"
          min-width="150"
          align="center"
        />
        <el-table-column key="phone" label="联系电话" prop="phone" min-width="150" align="center" />
        <el-table-column
          key="emergencyContact"
          label="紧急联系人"
          prop="emergencyContact"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="emergencyPhone"
          label="紧急联系人电话"
          prop="emergencyPhone"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="address"
          label="家庭住址"
          prop="address"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="photoUrl"
          label="照片URL"
          prop="photoUrl"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="department"
          label="部门/车间"
          prop="department"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="teamGroup"
          label="班组/产线"
          prop="teamGroup"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="position"
          label="岗位"
          prop="position"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="jobType"
          label="工种/工序"
          prop="jobType"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="skillLevel"
          label="技能等级"
          prop="skillLevel"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="specializedCategory"
          label="擅长服装类型"
          prop="specializedCategory"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="operableEquipment"
          label="会操作的设备"
          prop="operableEquipment"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="hireDate"
          label="入职日期"
          prop="hireDate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="contractStartDate"
          label="合同开始日期"
          prop="contractStartDate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="contractEndDate"
          label="合同结束日期"
          prop="contractEndDate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="employmentStatus"
          label="在职状态"
          prop="employmentStatus"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="resignationDate"
          label="离职日期"
          prop="resignationDate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="resignationReason"
          label="离职原因"
          prop="resignationReason"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="salaryType"
          label="计薪方式"
          prop="salaryType"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="baseSalary"
          label="底薪/保底工资"
          prop="baseSalary"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="pieceRate"
          label="计件单价"
          prop="pieceRate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="socialInsuranceNumber"
          label="社保号"
          prop="socialInsuranceNumber"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="bankAccount"
          label="银行账号"
          prop="bankAccount"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="dailyOutputAvg"
          label="日均产量"
          prop="dailyOutputAvg"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="qualityPassRate"
          label="质量合格率(%)"
          prop="qualityPassRate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="reworkRate"
          label="返工率(%)"
          prop="reworkRate"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="performanceRating"
          label="绩效评分"
          prop="performanceRating"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="createdAt"
          label="创建时间"
          prop="createdAt"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="updatedAt"
          label="更新时间"
          prop="updatedAt"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="createdBy"
          label="创建人"
          prop="createdBy"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="updatedBy"
          label="更新人"
          prop="updatedBy"
          min-width="150"
          align="center"
        />
        <el-table-column key="notes" label="备注" prop="notes" min-width="150" align="center" />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              v-hasPerm="['factory:factory-member:edit']"
              type="primary"
              size="small"
              link
              @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon><Edit /></template>
              编辑
            </el-button>
            <el-button
              v-hasPerm="['factory:factory-member:delete']"
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
            >
              <template #icon><Delete /></template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 服装厂员工信息表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="员工ID（主键）" prop="id">
          <el-input v-model="formData.id" placeholder="员工ID（主键）" />
        </el-form-item>

        <el-form-item label="员工编号/工号" prop="employeeId">
          <el-input v-model="formData.employeeId" placeholder="员工编号/工号" />
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="姓名" />
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-select v-model="formData.gender" placeholder="请选择性别">
            <el-option :value="0" label="选项一" />
            <el-option :value="1" label="选项二" />
          </el-select>
        </el-form-item>

        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="formData.idCard" placeholder="身份证号" />
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" placeholder="联系电话" />
        </el-form-item>

        <el-form-item label="紧急联系人" prop="emergencyContact">
          <el-input v-model="formData.emergencyContact" placeholder="紧急联系人" />
        </el-form-item>

        <el-form-item label="紧急联系人电话" prop="emergencyPhone">
          <el-input v-model="formData.emergencyPhone" placeholder="紧急联系人电话" />
        </el-form-item>

        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="formData.address" placeholder="家庭住址" />
        </el-form-item>

        <el-form-item label="照片URL" prop="photoUrl">
          <el-input v-model="formData.photoUrl" placeholder="照片URL" />
        </el-form-item>

        <el-form-item label="部门/车间" prop="department">
          <el-input v-model="formData.department" placeholder="部门/车间" />
        </el-form-item>

        <el-form-item label="班组/产线" prop="teamGroup">
          <el-input v-model="formData.teamGroup" placeholder="班组/产线" />
        </el-form-item>

        <el-form-item label="岗位" prop="position">
          <el-input v-model="formData.position" placeholder="岗位" />
        </el-form-item>

        <el-form-item label="工种/工序" prop="jobType">
          <el-input v-model="formData.jobType" placeholder="工种/工序" />
        </el-form-item>

        <el-form-item label="技能等级" prop="skillLevel">
          <el-input v-model="formData.skillLevel" placeholder="技能等级" />
        </el-form-item>

        <el-form-item label="擅长服装类型" prop="specializedCategory">
          <el-input v-model="formData.specializedCategory" placeholder="擅长服装类型" />
        </el-form-item>

        <el-form-item label="会操作的设备" prop="operableEquipment">
          <el-input v-model="formData.operableEquipment" placeholder="会操作的设备" />
        </el-form-item>

        <el-form-item label="入职日期" prop="hireDate">
          <el-date-picker
            v-model="formData.hireDate"
            class="!w-[240px]"
            type="date"
            placeholder="入职日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="合同开始日期" prop="contractStartDate">
          <el-date-picker
            v-model="formData.contractStartDate"
            class="!w-[240px]"
            type="date"
            placeholder="合同开始日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="合同结束日期" prop="contractEndDate">
          <el-date-picker
            v-model="formData.contractEndDate"
            class="!w-[240px]"
            type="date"
            placeholder="合同结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="在职状态" prop="employmentStatus">
          <el-input v-model="formData.employmentStatus" placeholder="在职状态" />
        </el-form-item>

        <el-form-item label="离职日期" prop="resignationDate">
          <el-date-picker
            v-model="formData.resignationDate"
            class="!w-[240px]"
            type="date"
            placeholder="离职日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="离职原因" prop="resignationReason">
          <el-input v-model="formData.resignationReason" placeholder="离职原因" />
        </el-form-item>

        <el-form-item label="计薪方式" prop="salaryType">
          <el-input v-model="formData.salaryType" placeholder="计薪方式" />
        </el-form-item>

        <el-form-item label="底薪/保底工资" prop="baseSalary">
          <el-input v-model="formData.baseSalary" placeholder="底薪/保底工资" />
        </el-form-item>

        <el-form-item label="计件单价" prop="pieceRate">
          <el-input v-model="formData.pieceRate" placeholder="计件单价" />
        </el-form-item>

        <el-form-item label="社保号" prop="socialInsuranceNumber">
          <el-input v-model="formData.socialInsuranceNumber" placeholder="社保号" />
        </el-form-item>

        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="formData.bankAccount" placeholder="银行账号" />
        </el-form-item>

        <el-form-item label="日均产量" prop="dailyOutputAvg">
          <el-input v-model="formData.dailyOutputAvg" placeholder="日均产量" />
        </el-form-item>

        <el-form-item label="质量合格率(%)" prop="qualityPassRate">
          <el-input v-model="formData.qualityPassRate" placeholder="质量合格率(%)" />
        </el-form-item>

        <el-form-item label="返工率(%)" prop="reworkRate">
          <el-input v-model="formData.reworkRate" placeholder="返工率(%)" />
        </el-form-item>

        <el-form-item label="绩效评分" prop="performanceRating">
          <el-input v-model="formData.performanceRating" placeholder="绩效评分" />
        </el-form-item>

        <el-form-item label="创建时间" prop="createdAt">
          <el-input v-model="formData.createdAt" placeholder="创建时间" />
        </el-form-item>

        <el-form-item label="更新时间" prop="updatedAt">
          <el-input v-model="formData.updatedAt" placeholder="更新时间" />
        </el-form-item>

        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="formData.createdBy" placeholder="创建人" />
        </el-form-item>

        <el-form-item label="更新人" prop="updatedBy">
          <el-input v-model="formData.updatedBy" placeholder="更新人" />
        </el-form-item>

        <el-form-item label="备注" prop="notes">
          <el-input v-model="formData.notes" placeholder="备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "FactoryMember",
  inheritAttrs: false,
});

import FactoryMemberAPI, {
  FactoryMemberPageVO,
  FactoryMemberForm,
  FactoryMemberPageQuery,
} from "@/api/factory/factory-member";

const queryFormRef = ref();
const dataFormRef = ref();

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<FactoryMemberPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 服装厂员工信息表格数据
const pageData = ref<FactoryMemberPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 服装厂员工信息表单数据
const formData = reactive<FactoryMemberForm>({});

// 服装厂员工信息表单校验规则
const rules = reactive({
  id: [{ required: true, message: "请输入员工ID（主键）", trigger: "blur" }],
  employeeId: [{ required: true, message: "请输入员工编号/工号", trigger: "blur" }],
  name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  department: [{ required: true, message: "请输入部门/车间", trigger: "blur" }],
  position: [{ required: true, message: "请输入岗位", trigger: "blur" }],
  hireDate: [{ required: true, message: "请输入入职日期", trigger: "blur" }],
  salaryType: [{ required: true, message: "请输入计薪方式", trigger: "blur" }],
});

/** 查询服装厂员工信息 */
function handleQuery() {
  loading.value = true;
  FactoryMemberAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置服装厂员工信息查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开服装厂员工信息弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改服装厂员工信息";
    FactoryMemberAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增服装厂员工信息";
  }
}

/** 提交服装厂员工信息表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        FactoryMemberAPI.update(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        FactoryMemberAPI.add(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭服装厂员工信息弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除服装厂员工信息 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      FactoryMemberAPI.deleteByIds(ids)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
